/*************************************************************************
  > File Name: 07.cover_binary.c
  > Author: icebear
  > Mail: 3322366195@qq.com
  > Created Time: Fri 14 Jul 2023 03:17:18 PM UTC
  将一个正整数转化成二进制输出
 ************************************************************************/

#include <stdio.h>
int main()
{
    while(1)
    {
        int decimal = 0;
        int temp = 0;
        int length =0;
        char binary[32];
        printf("input decimal number\n");
        scanf("%d",&decimal);
        if(decimal<0)
        {
            printf("error input negative\n\n");
            continue;
        }
            temp = decimal;
        while(temp!=0)
        {
            temp/=2;
            length++;
        }
        temp = decimal;
        for(int i=0;i<32&&length!=0;i++)
        {
            binary[length-i-1] = temp%2+48;
            temp/=2;
        }
        binary[length]='\0';
        printf("binary is %s\n\n",binary);
    }
}
